﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 谁与争锋可视化设计
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
           
            m_worker.WorkerReportsProgress = true;
            //m_worker.WorkerSupportsCancellation = true;

            //正式做事情的地方
            m_worker.DoWork += new DoWorkEventHandler(DoWork);

            //任务完称时要做的，比如提示等等
            m_worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);

            //任务进行时，报告进度
            m_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);

            //创建页面提取,分析对象,用户对象,统计对象,数据图形对象
            m_pageRequest = new PageRequest();
            m_pageAnalysis = new PageAnalysis();
            m_superior = new User();
            m_user = new User();
            m_userStatistics = new Statistics();
            m_superStatistics = new Statistics();
            m_dataGraph = new DataGraph();
                       
        }
   
        //调用 RunWorkerAsync 时发生
        public void DoWork(object sender, DoWorkEventArgs e)
        {    
            m_pageRequest.GetAcProblemPage();
        }

        //调用 ReportProgress 时发生
        public void ProgessChanged(object sender, ProgressChangedEventArgs e)
        {

            this.m_progressBar.Value = e.ProgressPercentage;
            this.label1.Text = e.ProgressPercentage.ToString() + "/" + m_pageRequest.AcProblemSum;
            this.label2.Text = e.UserState.ToString();

        }

        //当后台操作已完成、被取消或引发异常时发生
        public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("题目提取完成！");

            m_pageAnalysis.GetProblemNums(m_pageRequest.BaseMessagePage);

            //提取题目信息保存在用户对象中
            m_pageAnalysis.ExtractMessages(m_pageRequest.AcProblemPage, m_user, m_pageRequest.AcProblemSum);

            m_userStatistics.AnalysisDailyData(m_user);
            m_userStatistics.AnalysisMonthlyData();
    
            DateAnalysisTest dateAnalysisTest = new DateAnalysisTest();
            dateAnalysisTest.Form1 = this;       
            dateAnalysisTest.Show();
        }


        private void button2_Click(object sender, EventArgs e)
        {        
            button2.Enabled = false;

            //测试用户
            string userName = textBox1.Text.ToString();

            //pageRequest通过ExtractProgress对象通知进度条更新进度
            m_pageRequest.ExtractProgress = m_worker;
            m_pageRequest.GetBaseMessagePage(userName);

            //设置进度条最大值
            m_progressBar.Maximum = m_pageRequest.AcProblemSum;

            m_worker.RunWorkerAsync(); 
  
        }    
    }
}
